package cn.systemManage.action;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.basic.action.BasicAction;
import cn.systemManage.bean.UserBean;
import cn.systemManage.service.UserService;
import cn.util.CommonSendMsg;
import cn.util.ExceptionUtil;

@Controller
@Scope("prototype")
public class LoginAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(LoginAction.class);
	private UserBean userBean = new UserBean();
	@Resource UserService userService;
	/**
	 * 登录
	 * 1 成功
	 * 2 密码错误
	 * 3 验证码错误
	 * 4 用户不存在
	 * 5 其他情况  用户名为空 ..
	 * -1 后台错误
	 * 
	 */
	public void login() {
		String sofeCode = this.getRequestValue("sofeCode");
		String _sofeCode =  this.getSessionValueByKey("rand").toString();
		String msg = "";
		if(sofeCode.equals(_sofeCode)){
			try {
				msg = userService.login(userBean);
			} catch (Exception e) {
				logger.error(ExceptionUtil.getExceptionStack(e));
				e.printStackTrace();
				msg = "-1";
			}
		}else{
			msg = "3";
		}
		CommonSendMsg.writeMsg(msg);
	}
	
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return LOGIN;
	}
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public UserBean getUserBean() {
		return userBean;
	}
}
